var token = localStorage.getItem('tok');
var sfzh,phone,email,qq;
//身份证号
$(".sfzh").change(function(){
    sfzh=$('.sfzh').val();
   if(!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(sfzh))){  
        layer.msg('身份证号格式错误'); 
       return  false;  
   }
});
//手机号
$(".phone").change(function(){
    phone=$('.phone').val();
    if(!(/^1[34578]\d{9}$/.test(phone))){  
         layer.msg('手机号格式错误'); 
         kkgg=false;
        return  false;  
    }
 });
 //电子邮箱
 $(".email").change(function(){
    email=$('.email').val();
    if(!(/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(email))){  
         layer.msg('邮箱格式错误'); 
        return  false;  
    }
 });
 //qq号
 $(".qq").change(function(){
    qq=$('.qq').val();
    if(!(/^[1-9][0-9]{4,14}$/.test(qq))){  
         layer.msg('qq号格式错误');    
        return  false;  
    }
 });
var list1=[ ];
var list2=[];
var list3=[];
var list4=[];
var list5=[];
var list6=[];
var list7=[];
//个人信息
$(function () {
    _setMsg()
})
function _setMsg() {
    for(let i =1;i<=7;i++){
        _getMsg(i);
    }
}
function _getMsg(type) {
    $.ajax({
        type: "GET",
        url: $host+'/api/app/dictionary/getOptions',
        async: true,
        dataType:'json',
        data:{
            type:type,
            token:token
        },
        success: function(result){
            if(result.status===CUSTOM_ERR_OK){
                _insList(type,result.data)
            }
        },
        error:function(){
            console.log('接口错误')
        }
    });
}

function _insList(type,data) {
    var list = [];
    for (let i = 0;i<data.length;i++){
        list.push(data[i].value)
    }
    switch (type) {
        case 1:
            list1 = list
           break
        case 2:
            list2 = list
            break
        case 3:
            list3 = list
            break
        case 4:
            list4 = list
            break
        case 5:
            list5 = list
            break
        case 6:
            list6 = list
            break
        case 7:
            list7 = list
            break

    }
}
var i='';
var str1 = "";
var str2 = "";
var str3 = "";
var str4="";
var str5="";
var str6="";
var str7="";
//教育水平
$('.jysp').click(function(){
    $('.mask').show();
    listOne(list1,str1);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.jysp_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//婚姻状况
$('.hyzk').click(function(){
    $('.mask').show();
    listOne(list2,str2);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.hyzk_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//子女情况
$('.znqk').click(function(){
    $('.mask').show();
    listOne(list3,str3);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.znqk_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//居住时长
$('.jzsc').click(function(){
    $('.mask').show();
    listOne(list4,str4);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.jzsc_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//从事行业
$('.cshy').click(function(){
    $('.mask').show();
    listOne(list5,str5);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.cshy_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//亲属关系
$('.qsgx').click(function(){
    $('.mask').show();
    listOne(list6,str6);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.qsgx_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//社会关系
$('.shgx').click(function(){
    $('.mask').show();
    listOne(list7,str7);
    timer();
    var mySwiper = new Swiper('.swiperone.swiper-container',{
	    direction : 'vertical',
	    height:40,
	    initialSlide :1,
	    on: {
	    slideChangeTransitionEnd: function(){
            var a=$(".swiperone .swiper-wrapper .swiper-slide").eq(this.activeIndex).html();
            $('.shgx_a').text(a);
	    },
      },
      onDestroy: function(swiper){
        alert('你销毁了Swiper;');
            }
        })
        $('.fix_btn').click(function(){
        mySwiper.destroy(false)
  })  
})
//点击确定
$('.fix_btn').click(function(){
    $('.mask').hide();
})
function listOne(array,obj){
    let sw_height=array.length>=5 ? 5*1.65 : array.length*1.65
    $('.containers').height(sw_height+'rem');
	for(var i = 0;i<array.length;i++){
        obj += '<div class="swiper-slide">'+array[i]+'</div>' 
    }
    $(".swiperone .swiper-wrapper").html(obj);
}
function timer(){
    setTimeout(function(){
        $(".swiperone").css("padding-top","40px");
    },50)
}
//个人信息点击下一步
$('.grxx_btn').click(function(){
    var name=$('.xm').val();
    var cardNum=$('.sfzh').val();
    var phone=$('.phone').val();
    var email=$('.email').val();
    var qqNum=$('.qq').val();
    var educateLevel=$('.jysp_a').text();
    var marital=$('.hyzk_a').text();
    var children=$('.znqk_a').text();
    var address=$('.jtzz').val();
    var liveTime=$('.jzsc_a').text();
    if(name==""||cardNum==""||phone==""||email==""||qqNum==""||educateLevel==""||marital==""||children==""||address==""||liveTime==""){
        layer.msg('请完善您的信息');
        return
    }else if(!(/^1[34578]\d{9}$/.test(phone))||!(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(sfzh))||!(/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/.test(email))||!(/^[1-9][0-9]{4,14}$/.test(qq))){
        layer.msg('格式错误，请修改您的信息');
    }else{
        $.ajax({
            type: "POST",
            url: $host+'/api/app/person/addUserInfo',
            data: {
                "name":name,//姓名
                "cardNum":cardNum,//身份证
                "phone":phone,//手机号
                "email":email,//邮箱
                "qqNum":qqNum,//QQ
                "educateLevel":educateLevel,//教育水平
                "marital":marital,//婚姻状况
                "children":children,//子女状况
                "address":address,//家庭住址
                "liveTime":liveTime,//居住时长
                'token':token
            },
            async: true,
            dataType:'json',
            success: function(result){
                if(result.status==400018){
                    layer.msg("个人信息手机号与注册绑定的手机号不一致");
                }else{
                    $('.information_1').hide();
                    $('.information_2').hide();
                    $('.information_3').show();
                    $('.grxx_btn').hide();
                    $('.zyxx_btn').show();
                    $('.professionMsg>img').attr('src','../images/info_icon2@2x.png')
                    $('.professionMsg>p').addClass('active')
                }
            },
            error:function(){
                console.log('获取验证码错误');
            }
        });
    } 
})
//职业信息点击下一步
$('.zyxx_btn').click(function(){
    var industry=$('.cshy_a').text();
    var professional=$('.zymc').val();
    var company_name=$('.szgsmc').val();
    var company_address=$('.gsdz').val();
    var office_phone=$('.bgdh').val();
    var director=$('.gslxrxm').val();
    var director_phone=$('.gslxrdh').val();
    if(industry==""||professional==""||company_name==""||company_address==""||office_phone==""||director==""||director_phone==""){
        layer.msg('请完善您的信息');
        return
    }else{
        $.ajax({
            type: "POST",
            url: $host+'/api/app/person/addProfessionInfo',
            data: {
                "industry":industry,//从事行业
                "professional":professional,//职业名称
                "company_name":company_name,//公司名称
                "company_address":company_address,//公司地址
                "office_phone":office_phone,//	办公电话
                "director":director,//公司联系人
                "director_phone":director_phone,//	公司联系人电话
                'token':token
            },
            async: true,
            dataType:'json',
            success: function(result){
                $('.information_3').hide();
                $('.information_4').show();
                $('.information_5').show();
                $('.shgx_btn').show();
                $('.zyxx_btn').hide();
                $('.socialRelations>img').attr('src','../images/info_3@2x.png')
                $('.socialRelations>p').addClass('active')
            },
            error:function(){
                console.log('获取接口错误');
            }
        });
    }
})
// //社会关系点击下一步
$('.shgx_btn').click(function(){
    var kinship=$('.qsgx_a').text();
    var kinshipName=$('.qsxm').val();
    var kinshipPhone=$('.lxdh').val();
    var social=$('.shgx_a').text();
    var socialName=$('.lxrxxm').val();
    var socialPhone=$('.lxrdh').val();
    if(kinship==""||kinshipName==""||kinshipPhone==""||social==""||socialName==""||socialPhone==""){
        layer.msg('请完善您的信息');
    }else{
        $.ajax({
            type: "POST",
            url: $host+'/api/app/person/addSocialInfo',
            data: {
                "kinship":kinship,//亲属关系
                "kinshipName":kinshipName,//亲属名字
                "kinshipPhone":kinshipPhone,//亲属电话
                "social":social,//社会关系
                "socialName":socialName,//	社会关系名字
                "socialPhone":socialPhone,//社会关系电话
                'token':token
            },
            async: true,
            dataType:'json',
            success: function(result){
                if(result.status==900000){
                    window.location.href="tijiaosh.html?token="+token
                }
            },
            error:function(){
                console.log('获取接口错误');
            }
        });
    }
})
